package Server;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import Common.Settings;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

@SuppressWarnings("serial")
public class ServerConnectionGUI extends JFrame {

	private JPanel contentPane;
	private JTextField portTextField;
	private JTextField dbNameTextField;
	private JTextField dbUserTextField;
	private JTextField dbPasswordTextField;
	private JButton btnConnect;
	private JTextArea serverConsole ;
	private int connected=0;
	public EchoServer sv=null;
	private static ServerConnectionGUI serverConnectionGUI ;
	/**
	 * Create the frame.
	 */
	public ServerConnectionGUI() {
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(250, 30, 800, 600);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblDbUsername = new JLabel("DB User:");
		lblDbUsername.setForeground(new Color(160, 82, 45));
		lblDbUsername.setFont(new Font("Palatino Linotype", Font.BOLD | Font.ITALIC, 17));
		lblDbUsername.setBounds(171, 134, 68, 18);
		contentPane.add(lblDbUsername);
		
		JLabel lblDbPassword = new JLabel("DB Password:");
		lblDbPassword.setForeground(new Color(160, 82, 45));
		lblDbPassword.setFont(new Font("Palatino Linotype", Font.BOLD | Font.ITALIC, 17));
		lblDbPassword.setBounds(132, 163, 107, 18);
		contentPane.add(lblDbPassword);
		
		JLabel lblPort = new JLabel("Port:");
		lblPort.setForeground(new Color(160, 82, 45));
		lblPort.setFont(new Font("Palatino Linotype", Font.BOLD | Font.ITALIC, 17));
		lblPort.setBounds(201, 76, 38, 18);
		contentPane.add(lblPort);
		
		JLabel lblDbName = new JLabel("DB Scheme:");
		lblDbName.setForeground(new Color(160, 82, 45));
		lblDbName.setFont(new Font("Palatino Linotype", Font.BOLD | Font.ITALIC, 17));
		lblDbName.setBounds(148, 105, 91, 18);
		contentPane.add(lblDbName);
		
		portTextField = new JTextField();
		portTextField.setFont(new Font("Palatino Linotype", Font.PLAIN, 12));
		portTextField.setForeground(new Color(160, 82, 45));
		portTextField.setBounds(249, 75, 107, 20);
		contentPane.add(portTextField);
		portTextField.setColumns(10);
		portTextField.setText(Integer.toString(Settings.Default_Port));
		
		dbNameTextField = new JTextField();
		dbNameTextField.setForeground(new Color(160, 82, 45));
		dbNameTextField.setFont(new Font("Palatino Linotype", Font.PLAIN, 13));
		dbNameTextField.setBounds(249, 104, 107, 20);
		contentPane.add(dbNameTextField);
		dbNameTextField.setColumns(10);
		dbNameTextField.setText(Settings.DB_Scheme);
		
		dbUserTextField = new JTextField();
		dbUserTextField.setForeground(new Color(160, 82, 45));
		dbUserTextField.setFont(new Font("Palatino Linotype", Font.PLAIN, 13));
		dbUserTextField.setBounds(249, 133, 107, 20);
		contentPane.add(dbUserTextField);
		dbUserTextField.setColumns(10);
		dbUserTextField.setText(Settings.DB_UserName);
		
		dbPasswordTextField = new JTextField();
		dbPasswordTextField.setFont(new Font("Palatino Linotype", Font.PLAIN, 13));
		dbPasswordTextField.setForeground(new Color(160, 82, 45));
		dbPasswordTextField.setBounds(249, 162, 107, 20);
		contentPane.add(dbPasswordTextField);
		dbPasswordTextField.setColumns(10);
		dbPasswordTextField.setText(Settings.DB_Password);
		
		btnConnect = new JButton("Connect");
		btnConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				int port;
				
				if(connected==0){
					
					try
				    {
				      port = Integer.parseInt(portTextField.getText()); //Get port from command line
				  	  if ((portTextField.getText().isEmpty()) || dbNameTextField.getText().isEmpty() || dbUserTextField.getText().isEmpty() ) 
				  	  {
				  		JOptionPane.showMessageDialog(null, "ERROR: DB user/pass fields cannot be empty", "Warning Message", JOptionPane.ERROR_MESSAGE);
				  		return;
				  	  }
				      Settings.DB_UserName = dbUserTextField.getText();
				  	  Settings.DB_Password = dbPasswordTextField.getText();
				  	  Settings.DB_Scheme = dbNameTextField.getText();
				    }
				    catch(Throwable t)
				    {
				    	JOptionPane.showMessageDialog(null, "ERROR: Please check your input", "Warning Message", JOptionPane.ERROR_MESSAGE);
				      return;
				    }
					
					connected=1;
					if(sv==null)
						sv = new EchoServer(port,serverConnectionGUI);
					
				    if(sv.connect(port,sv)==1)
				    	btnConnect.setText("Stop Server");
				    else
				        JOptionPane.showMessageDialog(null, "ERROR: Cannot Start Server", "Warning Message", JOptionPane.ERROR_MESSAGE);
				}
				else{
					
					sv.disconnet(sv);
					connected=0;
					btnConnect.setText("Start Server");
				}
			
			 
			}
		});
		btnConnect.setForeground(new Color(160, 82, 45));
		btnConnect.setFont(new Font("Palatino Linotype", Font.BOLD | Font.ITALIC, 14));
		btnConnect.setBounds(213, 214, 122, 23);
		contentPane.add(btnConnect);
		
		JLabel lblServerConnection = new JLabel("Server Connection");
		lblServerConnection.setForeground(new Color(139, 0, 0));
		lblServerConnection.setFont(new Font("Snap ITC", Font.PLAIN, 22));
		lblServerConnection.setBounds(141, 22, 252, 29);
		contentPane.add(lblServerConnection);
		
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(35, 269, 416, 270);
		contentPane.add(scrollPane);
		
		serverConsole = new JTextArea();
		serverConsole.setForeground(new Color(0, 0, 139));
		serverConsole.setFont(new Font("Palatino Linotype", Font.BOLD, 13));
		serverConsole.setEditable(false);
		scrollPane.setViewportView(serverConsole);
		
		JLabel lblImageFrame = new JLabel("");
		lblImageFrame.setIcon(new ImageIcon(ServerConnectionGUI.class.getResource("/FramePictures/clientwindow.jpg")));
		lblImageFrame.setBounds(0, 0, 806, 574);
		contentPane.add(lblImageFrame);
	}
	
	
	public void display(String s)
	{
		serverConsole.append(s + "\n");
	}
	
	
	
	 public static void main(String[] args) 
	  {
			
	    //int port = 0; //Port to listen on
		 serverConnectionGUI= new ServerConnectionGUI(); // create main frame
		serverConnectionGUI.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		serverConnectionGUI.setVisible( true ); // display frame
	  }
}
